package il.technion.ewolf.kbr.openkad.msg;

import il.technion.ewolf.kbr.Node;

import java.io.Serializable;
import java.util.List;

import com.google.inject.name.Named;

/**
 * A findValue response as defined in the kademlia protocol
 * @author Ligal ligal2@gmail.com
 * @author Hen senderhen@gmail.com
 * we extend the find node response in order for nodes to be able to return the value from their 
 * storage.
 */
public class FindValueResponse extends KadResponse {

	private static final long serialVersionUID = 2103126060969733458L;
	private Serializable value;
	//nodes for continue searching
	private List<Node> nodes = null;
	private boolean storedResult;
	// is contains value or nodes
	private boolean result;
	
	
	
	protected FindValueResponse(long id, @Named("openkad.local.node") Node src) {
		super(id, src);
	}
	
	/***
	 * If the node contains the value of the key searched than return it
	 * @param p_value the value searched for
	 * @return the response message
	 */
	public FindValueResponse setValue (Serializable p_value) {
		this.value = p_value;
		return this;
	}
	
	/***
	 * @return The value stored for the key searched.
	 */
	public Serializable getValue() {
		return value;
	}
	
	/***
	 * If the nodes where stored in the cache 
	 * @param storedResult 
	 * @return the message
	 */
	public FindValueResponse setStoredResult(boolean storedResult) {
		this.storedResult = storedResult;
		return this;
	}
	
	/***
	 * @return If the nodes where stored in the cache
	 */
	public boolean isStoredResults() {
		return storedResult;
	}


	public boolean isContainingResult() {
		return result;
	}

	/***
	 * Sets an indicator which tells the receiver if the message contains a value
	 * @param p_result is the result in
	 * @return the message
	 */
	public FindValueResponse setContainingResult(boolean p_result) {
		this.result = p_result;
		return this;
	}

	/***
	 * If the node does not contains the value than it send the closest nodes it knows
	 * close to the node containing the value
	 * @param p_nodes 
	 * @return the message
	 */ 
	public FindValueResponse setNodes (List<Node> p_nodes) {
		this.nodes = p_nodes;
		return this;
	}
	
	/***
	 * @return The nodes closest to the node containing the value
	 */
	public List<Node> getNodes() {
		return nodes;
	}

}
